package com.rt.easyjava.base.biz1.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rt.easyjava.base.biz1.bean.TmptAuthDeptBiz;
import com.rt.easyjava.base.biz1.controller.base.TmptAuthDeptController;
import com.rt.easyjava.base.biz1.dto.TmptAuthDeptBizDto;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.base.UserDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端控制器
 * 1，自定义代码：源码生成器，创建模板，第一次生成源码时，会自动创建此类，之后生成源码时，此类将不做修改处理；
 *    所以，程序员应该在这个类里写自己的代码，后续需求变更，自动生成源码时，不影响此类。
 * 2，用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 * @author wuxiaoyong
 * @since 2020-07-17
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 */
@Slf4j
@Api(tags = "TmptAuthDeptBizController -  控制类")
@RequestMapping("/coding/tmptAuthDept")
@Controller
public class TmptAuthDeptBizController extends TmptAuthDeptController {
    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("分页查询列表（适合单表查询）")
    @GetMapping(value = "/page")
    @ResponseBody
    public Result page(@ModelAttribute TmptAuthDeptBizDto tmptAuthDeptBizDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "tmptAuthDept:query");
        return super.page(tmptAuthDeptBizDto, userDetailsDto);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("以ID作为父节点，查询子树")
    @GetMapping(value = "/tree")
    @ResponseBody
    //@Override
    public Result tree (Long id, String userDetailsJson) {
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "tmptAuthDept:query");

        Result<TmptAuthDeptBizDto> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        TmptAuthDeptBizDto tmptAuthDeptBizDto = new TmptAuthDeptBizDto();
        tmptAuthDeptBizDto.setId(0L);
        rebuildDataLimitQueryBean4Tree(tmptAuthDeptBizDto, userDetailsDto);
        if (id!=0) {
            TmptAuthDeptBiz tmptAuthDeptBiz = tmptAuthDeptBizService.getById(id);
            tmptAuthDeptBizDto = mapper.map(tmptAuthDeptBiz, TmptAuthDeptBizDto.class);
        }
        treeReduce(tmptAuthDeptBizDto);
        result.setObj(tmptAuthDeptBizDto);

        return result.success();
    }
    private void treeReduce(TmptAuthDeptBizDto tmptAuthDeptBizDto){
        // myson list
        QueryWrapper<TmptAuthDeptBiz> queryWrapper=new QueryWrapper<>();
        // queryWrapper.eq("tmpt_auth_dept_id", tmptAuthDeptBizDto.getId());
        queryWrapper.eq("parent_id", tmptAuthDeptBizDto.getId());
        queryWrapper.eq("deleteflag", 0);
        // queryWrapper.eq("mh_status", 1); // wxy
        queryWrapper.orderByAsc(true, "id");
        List<TmptAuthDeptBiz> sonList = tmptAuthDeptBizService.list(queryWrapper);
        if (sonList.size() == 0) {
            return;
        }
        List<TmptAuthDeptBizDto> sonDtoList = mapper.maplist(sonList, TmptAuthDeptBizDto.class);

        tmptAuthDeptBizDto.setTmptAuthDeptList(sonDtoList);
        for (TmptAuthDeptBizDto dto : sonDtoList) {
            treeReduce(dto);
        }
    }

    // 数据访问边界 0：全部数据， 10：机构内部数据
    public void rebuildDataLimitQueryBean4Tree(TmptAuthDeptBizDto tmptAuthDeptBizDto, UserDetailsDto userDetailsDto){
        if (userDetailsDto==null) return;
        int dataLimitType = userDetailsDto.getDataLimitType();
        if (dataLimitType == 10) {
            tmptAuthDeptBizDto.setId(Long.parseLong(userDetailsDto.getCompanyId()));
        }
        if(dataLimitType == 20){
            tmptAuthDeptBizDto.setCreatemanId(Long.parseLong(userDetailsDto.getUserId()));
        }
    }

    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("自定义sql分页查询（mapper.xml, 适合多表查询）")
    @GetMapping(value = "/myPage")
    @ResponseBody
    public Result myPage(@ModelAttribute TmptAuthDeptBizDto tmptAuthDeptBizDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "tmptAuthDept:query");
        return super.myPage(tmptAuthDeptBizDto, userDetailsDto);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("查询所有")
    @GetMapping(value = "/selectBy")
    @ResponseBody
    public Result selectBy(@ModelAttribute TmptAuthDeptBizDto tmptAuthDeptBizDto, String userDetailsJson){
        UserDetailsDto userDetailsDto = UserDetailsDto.getUserDetailsDto(userDetailsJson, "tmptAuthDept:query");
        return super.selectBy(tmptAuthDeptBizDto, userDetailsDto);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("selectById")
    @GetMapping(value = "/selectById")
    @ResponseBody
    @Override
    public Result selectById(Long id){
        return super.selectById(id);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:query')")
    @ApiOperation("SQL动态多选项, spring动态调用")
    @GetMapping(value = "/select4column")
    @ResponseBody
    @Override
    public Result select4column(String columnName){
        return super.select4column(columnName);
    }

    @PreAuthorize("hasAuthority('tmptAuthDept:create')")
    @ApiOperation("新增对象")
    @PostMapping(value = "/create")
    @ResponseBody
    @Override
    public Result create(@RequestBody TmptAuthDeptBizDto tmptAuthDeptBizDto,
                    String _userId, String _userName, String _companyId){
        return super.create(tmptAuthDeptBizDto, _userId, _userName, _companyId);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:update')")
    @ApiOperation("编辑对象")
    @PostMapping(value = "/update")
    @ResponseBody
    @Override
    public Result update(@RequestBody TmptAuthDeptBizDto tmptAuthDeptBizDto,
                        String _userId, String _userName){
        return super.update(tmptAuthDeptBizDto, _userId, _userName);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:delete')")
    @ApiOperation("逻辑删除对象，由deleteflag控制：0:有效，1:已删除。")
    @PostMapping(value = "/delete")
    @ResponseBody
    @Override
    public Result delete(Long id, String _userId, String _userName){
        return super.delete(id, _userId, _userName);
    }


    @PreAuthorize("hasAuthority('tmptAuthDept:deletePhysical')")
    @ApiOperation("物理删除对象，默认注释了核心调用方法，请按需启用")
    @PostMapping(value = "/deletePhysical")
    @ResponseBody
    @Override
    public Result deletePhysical(Long id){
        return super.deletePhysical(id);
    }

}
